home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C101.ZIP / UUPC11XS.ZIP / UUCICO / DCPLIB.C < prev    next >
C/C++ Source or Header  |  1992-11-12  |  13KB  |  314 lines

  1. /*
  2.    For best results in visual layout while viewing this file, set
  3.    tab stops to every 4 columns.
  4. */
  5.  
  6. /*
  7.    d c p l i b . c
  8.  
  9.    DCP system-dependent library
  10.  
  11.    Services provided by dcplib.c:
  12.  
  13.    - login
  14.    - UNIX commands simulation
  15.  
  16.    Updated:
  17.  
  18.       14May89  - Added system name to login prompt - ahd
  19.                  Added configuration file controlled user id, password
  20.                  Added Kermit server option
  21.       17May89  - Redo login processing to time out after five minutes;
  22.                  after all, we have to exit someday.                    ahd
  23.       22Sep89  - Add password file processing                           ahd
  24.       24Sep89  - Modify login() to issue only one wait command for up
  25.                  to 32K seconds; this cuts down LOGFILE clutter.        ahd
  26.       01Oct89  - Re-do function headers to allow copying for function
  27.                  prototypes in ulib.h                                   ahd
  28.       17Jan90  - Filter unprintable characters from logged userid and
  29.                  password to prevent premature end of file.             ahd
  30.       18Jan90  - Alter processing of alternate shells to directly
  31.                  invoke program instead of using system() call.         ahd
  32.    6  Sep 90   - Change logging of line data to printable               ahd
  33.       8 Sep 90 - Split ulib.c into dcplib.c and ulib.c                  ahd
  34.       8 Oct 90 - Break rmail.com and rnews.com out of uuio
  35.                  Add FIXED_SPEED option for no-autobauding              ahd
  36.       10Nov 90 - Move sleep call into ssleep and rename                 ahd
  37. */
  38.  
  39. #include <ctype.h>
  40. #include <direct.h>
  41. #include <dos.h>
  42. #include <process.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <sys/types.h>
  47. #include <time.h>
  48.  
  49. #ifdef __TURBOC__
  50. #include <sys/timeb.h>
  51. #endif
  52.  
  53. #include "lib.h"
  54. #include "arpadate.h"
  55. #include "dcp.h"
  56. #include "dcplib.h"
  57. #include "dcpsys.h"
  58. #include "hlib.h"
  59. #include "hostable.h"
  60. #include "import.h"
  61. #include "modem.h"
  62. #include "pushpop.h"
  63. #include "security.h"
  64. #include "ssleep.h"
  65. #include "ulib.h"
  66. #include "usertabl.h"
  67. #include "timestmp.h"
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*        Define current file name for panic() and printerr()         */
  71. /*--------------------------------------------------------------------*/
  72.  
  73. currentfile();
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*                    Internal function prototypes                    */
  77. /*--------------------------------------------------------------------*/
  78.  
  79. static void LoginShell( const   struct UserTable *userp );
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*    l o g i n                                                       */
  83. /*                                                                    */
  84. /*    Login handler                                                   */
  85. /*--------------------------------------------------------------------*/
  86.  
  87. boolean login(void)
  88. {
  89.    char line[BUFSIZ];                  /* Allow for long domain names!  */
  90.    char user[50];
  91.    char pswd[50];
  92.    char attempts = 0;                  /* Allows login tries         */
  93.    char *token;                        /* Pointer to returned token  */
  94.    struct UserTable *userp;
  95.  
  96.  
  97. /*--------------------------------------------------------------------*/
  98. /*    Our modem is now connected.  Begin actual login processing      */
  99. /*    by displaying a banner.                                         */
  100. /*--------------------------------------------------------------------*/
  101.  
  102.    ssleep(1);
  103.    sprintf(line,"\r\n\n%s(R) %d.%02d with %s %s (%s) (%s)\r\n",
  104. #ifdef __TURBOC__
  105.             "MS-DOS",
  106.             _osmajor, _osminor,
  107. #else
  108.             (_osmode == DOS_MODE) ? "MS-DOS" : "OS/2" ,
  109.             (_osmode == DOS_MODE) ? _osmajor : ((int) _osmajor / 10 ),
  110.             _osminor,
  111. #endif
  112.        compilep,
  113.        compilev,
  114.        E_domain, device); /* Print a hello message            */
  115.    wmsg(line,0);
  116.    ddelay(250);
  117.  
  118. /*--------------------------------------------------------------------*/
  119. /*    Display a login prompt until we get a printable character or    */
  120. /*    the login times out                                             */
  121. /*--------------------------------------------------------------------*/
  122.  
  123.    for ( attempts = 0; attempts < 5 ; attempts++ )
  124.    {
  125.       boolean invalid = TRUE;
  126.       while (invalid)         /* Spin for a user id or timeout       */
  127.       {
  128.          wmsg("\r\nlogin: ", 0);
  129.          strcpy(user,"");
  130.          if (rmsg(user, 2, 30, sizeof user) == TIMEOUT)
  131.                                     /* Did the user enter data?  */
  132.             return FALSE;   /* No --> Give up                */
  133.  
  134.          if (equal(user,"NO CARRIER"))
  135.             return FALSE;
  136.  
  137.          token = user;
  138.          while ((*token != '\0') && invalid) /* Ignore empty lines   */
  139.             invalid = ! isgraph(*token++);
  140.       } /* while */
  141.  
  142.       printmsg(14, "login: login=%s", user);
  143.  
  144. /*--------------------------------------------------------------------*/
  145. /*               We have a user id, now get a password                */
  146. /*--------------------------------------------------------------------*/
  147.  
  148.       wmsg("\r\nPassword: ", 0);
  149.       strcpy(pswd,"");
  150.       if (rmsg(pswd, 0, 30, sizeof pswd) == TIMEOUT)
  151.          return FALSE;
  152.  
  153. /*--------------------------------------------------------------------*/
  154. /*       Zap unprintable characters before we log the password        */
  155. /*--------------------------------------------------------------------*/
  156.  
  157.       printmsg(14, "login: password=%s", pswd);
  158.  
  159. /*--------------------------------------------------------------------*/
  160. /*                 Validate the user id and passowrd                  */
  161. /*--------------------------------------------------------------------*/
  162.  
  163.       userp = checkuser(user);         /* Locate user id in host table  */
  164.  
  165.       if (userp == BADUSER)            /* Does user id exist?           */
  166.       {                                /* No --> Notify the user        */
  167.          wmsg("\r\nlogin failed",0);
  168.  
  169.          token = user;
  170.          while (!isalnum( *token ) && (*token !=  '\0'))
  171.             token ++;                  /* Scan for first alpha-numeric  */
  172.  
  173.          if (*token != '\0')           /* If at least one good char     */
  174.             printmsg(0,"login: login for user %s failed, bad user id",
  175.                   user);               /* Log the error for ourselves   */
  176.       }
  177.       else if ( equal(pswd,userp->password))   /* Correct password?     */
  178.       {                                /* Yes --> Log the user "in"     */
  179.                    /*   . . ..+....1....  +....2....+....3....  + .   */
  180.          sprintf(line,"\r\n\nWelcome to %s; login complete at %s\r\n",
  181.                   E_domain, arpadate());
  182.          wmsg(line, 0);
  183.          printmsg(0,"login: login user %s (%s) at %s",
  184.                      userp->uid, userp->realname, arpadate());
  185.  
  186.          if equal(userp->sh,UUCPSHELL) /* Standard uucp shell?       */
  187.          {
  188.             securep = userp->hsecure;
  189.             printmsg(5,"Processing user via %s", UUCPSHELL);
  190.             return TRUE;            /* Yes --> Startup the machine   */
  191.          }
  192.          else {                     /* No --> run special shell      */
  193.             LoginShell( userp );
  194.             return FALSE;   /* Hang up phone and exit        */
  195.          }
  196.       }
  197.       else {                        /* Password was wrong.  Report   */
  198.          wmsg("\r\nlogin failed",0);
  199.          printmsg(0,"login: login user %s (%s) failed, bad password %s",
  200.                   userp->uid, userp->realname, pswd);
  201.       }
  202.    }  /* for */
  203.  
  204. /*-----------------------------------------------------------------*/
  205. /*    If we fall through the loop, we have an excessive number of  */
  206. /*    login attempts; hangup the telephone and try again.          */
  207. /*-----------------------------------------------------------------*/
  208.  
  209.    return FALSE;                    /* Exit processing            */
  210.  
  211. } /*login*/
  212.  
  213. /*--------------------------------------------------------------------*/
  214. /*    l o g i n b y p a s s                                           */
  215. /*                                                                    */
  216. /*    Initialize user setup when login is bypassed                    */
  217. /*--------------------------------------------------------------------*/
  218.  
  219. boolean loginbypass(const char *user)
  220. {
  221.    struct UserTable *userp;
  222.    char line[BUFSIZ];                  /* Allow for long domain names!  */
  223.  
  224.    printmsg(14, "loginbypass: login=%s", user);
  225.  
  226. /*--------------------------------------------------------------------*/
  227. /*                 Validate the user id                               */
  228. /*--------------------------------------------------------------------*/
  229.  
  230.    userp = checkuser(user);         /* Locate user id in host table  */
  231.  
  232.    if (userp == BADUSER)            /* Does user id exist?           */
  233.    {                                /* No --> Notify the user        */
  234.       wmsg("\r\nUUCICO login failed",0);
  235.  
  236.       printmsg(0,"loginbypass: login for user %s failed, bad user id",
  237.                user);               /* Log the error for ourselves   */
  238.       return FALSE;                 /* Hang up phone and exit        */
  239.    }
  240.    else {
  241.                                     /* Yes --> Log the user "in"     */
  242.                 /*   . . ..+....1....  +....2....+....3....  + .   */
  243.       sprintf(line,"\r\n\nWelcome to %s; login complete at %s\r\n",
  244.                E_domain, arpadate());
  245.       wmsg(line, 0);
  246.       printmsg(0,"loginbypass: login user %s (%s) at %s",
  247.                   userp->uid, userp->realname, arpadate());
  248.  
  249.       if equal(userp->sh,UUCPSHELL) /* Standard uucp shell?       */
  250.       {
  251.          securep = userp->hsecure;
  252.          return TRUE;            /* Yes --> Startup the machine   */
  253.       } /* if equal(userp->sh,UUCPSHELL) */
  254.       else {                     /* No --> run special shell      */
  255.          LoginShell( userp );
  256.          return FALSE;           /* Hang up phone and exit        */
  257.       } /* else */
  258.    } /* else */
  259.  
  260. } /*loginbypass*/
  261.  
  262. /*--------------------------------------------------------------------*/
  263. /*    L o g i n S h e l l                                             */
  264. /*                                                                    */
  265. /*    Execute a non-default remote user shell                         */
  266. /*--------------------------------------------------------------------*/
  267.  
  268. static void LoginShell( const   struct UserTable *userp )
  269. {
  270.    char *shellstring;
  271.    char *path;
  272.    char *args;
  273.    int   rc;
  274.  
  275. /*--------------------------------------------------------------------*/
  276. /*              Get the program to run and its arguments              */
  277. /*--------------------------------------------------------------------*/
  278.  
  279.    shellstring = strdup(userp->sh);
  280.                            /* Copy user shell for parsing   */
  281.    path = strtok(shellstring," \t");   /* Get program name  */
  282.    args = strtok(NULL,"");    /* Get rest of arg string     */
  283.  
  284.    printmsg(1,"LoginShell: Invoking %s in directory %s",
  285.          userp->sh, userp->homedir);
  286.  
  287.    ddelay(250);            /* Wait for port to stablize     */
  288.  
  289. /*--------------------------------------------------------------------*/
  290. /*       Run the requested program in the user's home directory       */
  291. /*--------------------------------------------------------------------*/
  292.  
  293.    PushDir(userp->homedir);/* Switch to user's home dir     */
  294.    if (args == NULL)
  295.       rc = spawnl(P_WAIT, path, path, NULL);
  296.    else
  297.       rc = spawnl(P_WAIT, path, path, args, NULL);
  298.  
  299.    PopDir();               /* Return to original directory  */
  300.  
  301. /*--------------------------------------------------------------------*/
  302. /*                     Report any errors we found                     */
  303. /*--------------------------------------------------------------------*/
  304.  
  305.    if ( rc < 0 )           /* Error condition?              */
  306.    {                        /* Yes --> Report it to the user */
  307.       printmsg(0,"LoginShell: Unable to execute user shell");
  308.       printerr(path);
  309.    }
  310.    else                    /* No --> Report normal result   */
  311.       printmsg(rc == 0 ? 4 : 0,"LoginShell: %s return code is %d", path, rc);
  312.  
  313. } /* LoginShell */
  314.